package com.study.interceptor;

import com.study.web.LoginController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * JSOUP 测试
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 在目标方法执行之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURI = request.getRequestURI();
        log.info("|拦截器|URL={}|{}", request.getRequestURL(), requestURI);
        //TODO  需要拦截的地方
        HttpSession session = request.getSession();

        if (requestURI.contains("/jsoup/pri/")) {
            String jsoup = (String) session.getAttribute("JSOUP");
            if (StringUtils.isEmpty(jsoup)) {
                if (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest")) {// 是ajax请求
                    //response.setCharacterEncoding("text/html;charset=UTF-8");
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("text/html;charset=UTF-8");
                    PrintWriter writer = response.getWriter();
                    StringBuffer jsonTip = new StringBuffer("{\"result\":");
                    jsonTip.append(false).append(",\"tip\":\"您无权操作\"}");
                    writer.print(jsonTip.toString());
                    return false;
                } else {// 非ajax请求
                    String content = request.getSession().getServletContext().getContextPath();
                    // response.sendRedirect(content + "/refuse.html");//转发到页面
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("您无权操作呀！");
                    return false;
                }
            }
            System.out.println("SESSION:JSOUP=" + jsoup);
        }
        //TODO 登录测试
        if (requestURI.contains("/login")) {
            String jsoup = (String) session.getAttribute(LoginController.SESSION_ID);
            log.info("loginName {}", jsoup);
            if (StringUtils.isEmpty(jsoup)) {
                if (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest")) {// 是ajax请求
                    //response.setCharacterEncoding("text/html;charset=UTF-8");
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("text/html;charset=UTF-8");
                    PrintWriter writer = response.getWriter();
                    StringBuffer jsonTip = new StringBuffer("{\"result\":");
                    jsonTip.append(false).append(",\"tip\":\"您无权操作\"}");
                    writer.print(jsonTip.toString());
                    return false;
                } else {// 非ajax请求
                    String content = request.getSession().getServletContext().getContextPath();
                    // response.sendRedirect(content + "/refuse.html");//转发到页面
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("您无权操作呀！");
                    return false;
                }
            }
            System.out.println("SESSION:JSOUP=" + jsoup);
        }
        return true;
    }
}
